/*
 * Copyright (C) 2012 Martin Řehánek
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cz.mzk.funnylib.api.builders;

import cz.mzk.funnylib.dao.UserDAO;
import cz.mzk.funnylib.dao.db.UserDAOImpl;
import cz.mzk.funnylib.entities.User;

/**
 *
 * @author Martin Řehánek
 */
public class UserBuilder extends AbstractBuilder {

    private UserDAO dao = new UserDAOImpl(getJdbcTemplate());

    public String buildUserById(long id) {
        //TODO: usera z databaze
//        User testUser = mockUser(id);
//        return buildUserById(testUser);

        return buildUser(dao.getUserById(id));
    }

    public String buildUserByLogin(String login) {
        //TODO: usera z databaze
//        User testUser = mockUser(id);
//        return buildUserById(testUser);

        return buildUser(dao.getUserByLogin(login));
    }

    public String buildUser(User user) {
        StringBuilder result = new StringBuilder();
        result.append("{");
        result.append("\"").append("id").append("\"").append(":");
        result.append("\"").append(user.getId()).append("\"");
        result.append(",");
        result.append("\"").append("name").append("\"").append(":");
        result.append("\"").append(user.getName()).append("\"");
        result.append(",");
        result.append("\"").append("surname").append("\"").append(":");
        result.append("\"").append(user.getSurname()).append("\"");
        result.append(",");
        result.append("\"").append("login").append("\"").append(":");
        result.append("\"").append(user.getLogin()).append("\"");
        result.append(",");
        result.append("\"").append("password").append("\"").append(":");
        result.append("\"").append(user.getPassword()).append("\"");
        result.append("}");
        return result.toString();
    }

    public User mockUser(long id) {
        User result = new User();
        result.setId(id);
        result.setName("Josef");
        result.setSurname("Novák");
        result.setLogin("novak");
        result.setPassword("heslo");
        return result;
    }
}
